package com.ruoyi.web.controller.repair;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.DataUtils;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.service.IHouseDoorTService;
import com.ruoyi.project.domain.HouseProjectT;
import com.ruoyi.project.domain.HouseRepairCheckT;
import com.ruoyi.project.service.IHouseProjectTService;
import com.ruoyi.repairDoor.domain.HouseRepairDoorT;
import com.ruoyi.repairDoor.service.IHouseRepairDoorTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 户资金修改Controller
 *
 * @author tyq
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/repair/repairDoor")
public class HouseRepairDoorTController extends BaseController {
    @Autowired
    private IHouseRepairDoorTService houseRepairDoorTService;
    private HouseDoorT doorVO = new HouseDoorT();
    private HouseProjectT projectVO = new HouseProjectT();

    private List<HouseRepairDoorT> houseRepairDoorList = new ArrayList<HouseRepairDoorT>();
    private List<HouseRepairDoorT> repairListTemp = new ArrayList<HouseRepairDoorT>();


    private List<HouseProjectT> houseProjectList = new ArrayList<HouseProjectT>();
    @Autowired
    private IHouseProjectTService houseProjectTService;
    @Autowired
    private IHouseDoorTService houseDoorTService;
    private String projectId;

    public HouseRepairDoorTController(IHouseProjectTService houseProjectTService) {
        this.houseProjectTService = houseProjectTService;
    }

    /**
     * 查询维修列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseRepairDoorT houseRepairDoorT) {
        HouseRepairDoorT houseRepairDoorVO=new HouseRepairDoorT();
        projectId= houseRepairDoorT.getProjectId();
        List<HouseDoorT> doorList = new ArrayList<HouseDoorT>();
        if(projectId!=null){
            projectVO=houseProjectTService.selectHouseProjectTByProjectId(projectId);
            if(projectVO!=null && "1".equals(projectVO.getProjectSts())){
                return getDataTable(doorList);
            }
            doorVO.setAreaName(projectVO.getAreaName());
            doorVO.setGroupName(projectVO.getGroupName());
            doorVO.setUnitName(projectVO.getUnitName());
            startPage();
            doorList = houseDoorTService.selectHouseDoorTList(doorVO);
        }
        return getDataTable(doorList);
    }

    @GetMapping (value = "/rightList")
    public TableDataInfo rightList(HouseRepairDoorT houseRepairDoorT) {

        HouseRepairDoorT houseRepairDoorVO=new HouseRepairDoorT();
        List<HouseRepairDoorT> list = new ArrayList<HouseRepairDoorT>();
        projectId= houseRepairDoorT.getProjectId();
        projectVO=houseProjectTService.selectHouseProjectTByProjectId(projectId);
		if(projectVO!=null){
            if(!(projectVO!=null && "1".equals(projectVO.getProjectSts())&&"4".equals(projectVO.getProjectType()))){
                list = houseRepairDoorTService.selectHouseRepairDoorTList(houseRepairDoorT);
                if(getDataTableSumJudge(list)){
                    houseRepairDoorVO = houseRepairDoorTService.selectHouseRepairDoorSum(houseRepairDoorT);
                    houseRepairDoorVO.setHouseId("合计");
                    list.add(houseRepairDoorVO);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出户资金修改列表
     */
    @Log(title = "小区维修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseRepairDoorT houseRepairDoorT) {
        List<HouseRepairDoorT> list = houseRepairDoorTService.selectHouseRepairDoorTList(houseRepairDoorT);
        ExcelUtil<HouseRepairDoorT> util = new ExcelUtil<HouseRepairDoorT>(HouseRepairDoorT.class);
        util.exportExcel(response, list, "户资金修改数据");
    }

    /**
     * 获取户资金修改详细信息
     */

    @GetMapping(value = "/{houseId}/{projectId}")
    public AjaxResult getInfo(@PathVariable("houseId") String houseId,@PathVariable("projectId") String projectId) {
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        houseRepairDoorVO.setHouseId(houseId);
        houseRepairDoorVO.setProjectId(projectId);
        return AjaxResult.success(houseRepairDoorTService.getRepairDetail(houseRepairDoorVO));
    }


    /**
     * 新增户资金修改
     */
    @Log(title = "新增", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/insertRepairDoor/{houseIds}/{projectId}")
    public AjaxResult insertRepairDoor(@PathVariable("houseIds") String[] houseIds,@PathVariable("projectId") String projectId) {
        for(String houseId:houseIds){
            HouseRepairDoorT vo=new HouseRepairDoorT();
            HouseRepairDoorT voTemp=new HouseRepairDoorT();
            vo.setHouseId(houseId);
            vo.setProjectId(projectId);
            voTemp=houseRepairDoorTService.selectRepairQuery(vo);
            if(voTemp!=null){
                return AjaxResult.error("该维修列表下以存在此用户,请在右侧输入工程编号进行搜索");
            }
        }
        return toAjax(houseRepairDoorTService.insertHouseRepairDoorT(houseIds,projectId));
    }

    /**
     * 删除
     */
    @Log(title = "删除", businessType = BusinessType.UPDATE)
    @GetMapping("/delRepairDoor/{houseIds}/{projectId}")
    public AjaxResult deleteRepair(@PathVariable("houseIds") String[] houseIds,@PathVariable("projectId") String projectId) {
        return toAjax(houseRepairDoorTService.deleteHouseRepairDoorTByDoorIds(houseIds,projectId));
    }

    /**
     * 修改户资金修改
     */
    @Log(title = "户资金修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseRepairDoorT houseRepairDoorT) {
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        HouseRepairCheckT houseRepairCheckO = new HouseRepairCheckT();
        HouseProjectT houseProjectT = new HouseProjectT();
        houseRepairDoorVO = houseRepairDoorTService.getRepairDetail(houseRepairDoorT);
        houseProjectT.setProjectId(houseRepairDoorT.getProjectId());
        houseProjectT.setRepairMoney(houseRepairDoorVO.getRepairMoney());
        houseProjectT.setCheckMoney(houseRepairDoorT.getRepairMoney());
        houseRepairDoorVO.setRepairMoney(houseRepairDoorT.getRepairMoney());
        if(houseRepairDoorT.getRepairMoney() == 0){
            houseRepairDoorVO.setNoMoney(0d);
        }else if(houseRepairDoorVO.getBalance() <= houseRepairDoorVO.getBudgetMoney()*0.01){
            houseRepairDoorVO.setNoMoney(houseRepairDoorT.getRepairMoney());
        }else if(houseRepairDoorVO.getBalance() - houseRepairDoorT.getRepairMoney() >= houseRepairDoorVO.getBudgetMoney()*0.01){
            houseRepairDoorVO.setNoMoney(0d);
        }else{
            houseRepairDoorVO.setNoMoney(DataUtils.doubleScale2(houseRepairDoorT.getRepairMoney() - (houseRepairDoorVO.getBalance() - houseRepairDoorVO.getBudgetMoney()*0.01)) );
        }
        houseRepairCheckO.setHouseId(houseRepairDoorVO.getHouseId());
        houseRepairCheckO.setProjectId(houseRepairDoorVO.getProjectId());
        houseRepairCheckO.setRepairMoney(houseRepairDoorVO.getRepairMoney());
        houseRepairCheckO.setUpdateMoney(houseRepairDoorT.getRepairMoney());
        houseRepairCheckO.setUpdater(getUsername());

        return toAjax(houseRepairDoorTService.updateHouseRepairDoorT(houseRepairDoorVO,houseRepairCheckO,houseProjectT));
    }

    @GetMapping (value = "/getCalculate")
    public AjaxResult getCalculateByProjectId(HouseProjectT houseProjectT) {
        HouseProjectT houseProjectVO = new HouseProjectT();
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        Double trueMoney = 0d;
        Double spaceSum = 0d;
        String projectId = houseProjectT.getProjectId();
        Double price = houseProjectT.getPrice();
        houseProjectVO = houseProjectTService.selectHouseProjectTByProjectId(projectId);
        houseRepairDoorVO.setProjectId(projectId);
        List<HouseRepairDoorT> houseRepairDoorList = houseRepairDoorTService.selectHouseRepairDoorTList(houseRepairDoorVO);
        for(HouseRepairDoorT vo:houseRepairDoorList){
            if(vo.getSpace()==0){
                String houseid=vo.getHouseId();
                return AjaxResult.error(houseid+"该房屋编号面积为0,不能产生分摊");
            }
            spaceSum = DataUtils.sumMoney(spaceSum,vo.getSpace());
        }
        doorVO.setAreaName(houseProjectVO.getAreaName());
        doorVO = houseDoorTService.getBalanceMoneyAll(doorVO);
        if(price==null){
            price= DataUtils.divideMoney(houseProjectVO.getProjectMoney(),spaceSum);
        }
//        BigDecimal price =(BigDecimal.valueOf(houseProjectVO.getProjectMoney())).divide(BigDecimal.valueOf(doorVO.getSpace()),6,BigDecimal.ROUND_HALF_UP);
        for (HouseRepairDoorT vo : houseRepairDoorList){
            Double repairMoney= DataUtils.spaceMoney(vo.getSpace(),price);
            trueMoney = DataUtils.sumMoney(trueMoney,  repairMoney);
        }
        houseProjectVO.setTrueMoney(trueMoney);
        houseProjectVO.setPrice(price);
        houseProjectVO.setSpace(doorVO.getSpace());
        return AjaxResult.success(houseProjectVO);
    }


    /**
     * 核算
     */
    @Log(title = "核算", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCalculate")
    public AjaxResult updateCalculate(@RequestBody HouseProjectT houseProjectT) {
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        houseRepairDoorVO.setProjectId(houseProjectT.getProjectId());
        Double price = Double.valueOf(houseProjectT.getPrice());
        Double trueMoney = 0d;
        List<HouseRepairDoorT> houseRepairDoorList = houseRepairDoorTService.selectHouseRepairDoorTList(houseRepairDoorVO);
        for(HouseRepairDoorT vo:houseRepairDoorList){
            HouseRepairDoorT temp =new HouseRepairDoorT();
            Double repaireMoney = 0d;
            repaireMoney = DataUtils.doubleScale2(vo.getSpace()*price);
            if(vo.getBalance()- vo.getBudgetMoney()*0.3<=0){
                temp.setNoMoney(DataUtils.doubleScale2(repaireMoney));
            }else if(vo.getBalance()- vo.getBudgetMoney()*0.3>=repaireMoney){
                temp.setNoMoney(0d);
            }else{
                temp.setNoMoney(DataUtils.doubleScale2(repaireMoney-vo.getBalance()+vo.getBudgetMoney()*0.3));
            }
            temp.setProjectId(houseRepairDoorVO.getProjectId());
            temp.setHouseId(vo.getHouseId());
            temp.setRepairMoney(repaireMoney);
            temp.setPayMoney(repaireMoney-temp.getNoMoney());
            trueMoney = DataUtils.sumMoney(trueMoney, repaireMoney);
            repairListTemp.add(temp);
        }
        projectVO.setTrueMoney(Double.valueOf(trueMoney.toString()));
        projectVO.setPayMoney(Double.valueOf(trueMoney.toString()));
        projectVO.setProjectId(houseRepairDoorVO.getProjectId());
        houseRepairDoorTService.updateProjectAndRepair(projectVO,repairListTemp);

        return toAjax(1);
    }


}
